package com.tuo51.childcare.service.impl;

import com.tuo51.childcare.domain.carestore.StoreItem;
import com.tuo51.childcare.repository.StoreItemRepository;
import com.tuo51.childcare.service.BizStoreItemService;
import com.tuo51.evaluate.domain.tags.RelObjectTag;
import com.tuo51.evaluate.repositories.tags.RelObjectTagRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.tuo51.foudation.constant.APPConstants.TagRelObjectType.STOREITEM;

@Service
public class BizStoreItemServiceImpl implements BizStoreItemService {

    @Autowired
    RelObjectTagRepository relObjectTagRepository;

    @Autowired
    StoreItemRepository storeItemRepository;

    @Override
    public List<StoreItem> getStoreItemByObjects(List<String> objectIds, Integer objectType, Integer month) {

        List<RelObjectTag> rels = relObjectTagRepository.findAllBySourceObjectIdsAndType(objectIds, objectType, STOREITEM.getValue());

        List<String> itemIds = rels.stream().filter(o -> StringUtils.isNotEmpty(o.getObjId())).map(o -> {
            return o.getObjId();
        }).collect(Collectors.toList());

        List<StoreItem> ret = storeItemRepository.findAllByIdIn(itemIds);

        return ret;
    }
}
